package com.gack.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gack.business.dao.CameraDao;
import com.gack.business.model.StoreCameras;
import com.gack.business.model.VideoStores;
import com.gack.business.repository.StoreCamerasRepository;
import com.gack.business.repository.VideoStoresRepository;

/**
 * 
* @ClassName: StoreCameraService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年7月11日 上午8:48:02 
* @version V1.0
 */
@Service
public class StoreCameraService implements StoreCameraServiceInterface{

	@Autowired
	private StoreCamerasRepository cameraRepository;
	@Autowired
	private CameraDao cameraDao;
	@Autowired
	private BackLogServiceInterface backLogService;
	@Autowired
	private VideoStoresRepository storeRepository;
	
	@Override
	public Map<String, Object> getNoCameraStores(String cameraId,String province,String city,String area) {
		Map<String, Object> map = new HashMap<>();
		List<VideoStores> stores = cameraDao.getNoCameraStores(cameraId, province, city, area);
		map.put("key", "success");
		map.put("stores", stores);
		return map;
	}

	@Override
	public Map<String, Object> addOrUpdate(String backid,String id, String storeId, String chineseName, String meetingRoomId,
			Integer type, String ip) {
		Map<String, Object> map = new HashMap<>();
		StoreCameras camera = null;
		if(id != null && !id.equals("")){
			camera = cameraRepository.findOne(id);
			if(camera == null ){
				map.put("key", "error");
				map.put("msg", "设备不存在");
				return map;
			}
		}else{
			camera = new StoreCameras();
			camera.setCreateTime(new Date());
			camera.setStatus(0);
			camera.setIsUse(0);
		}
		if(cameraRepository.getTotalStoreCameraCount(storeId) > 0){
			map.put("key", "error");
			map.put("msg", "该门店已存在设备");
			return map;
		}
		if(cameraRepository.getStoreCameraTotal(chineseName) > 0){
			map.put("key", "error");
			map.put("msg", "该名称已存在");
			return map;
		}
		camera.setStoreId(storeId);
		camera.setChineseName(chineseName);
		camera.setMeetingRoomId(meetingRoomId);
		camera.setType(type);
		camera.setIp(ip);
		camera = cameraRepository.save(camera);
		backLogService.addBackLog(backid, "添加/修改摄像头>> 设备id："+camera.getId()+" 操作："+(id == null || id.equals("") ? "添加" : "更新"));
		map.put("key", "success");
		map.put("camera", camera);
		map.put("status", "添加/修改设备成功");
		return map;
	}

	@Override
	public Map<String, Object> getCameras(String province, String city, String area, String storeName,String chineseNmae,Integer page,Integer size) {
		Map<String, Object> map = new HashMap<>();
		List<StoreCameras> cameras = cameraDao.getCameras(province, city, area, storeName, chineseNmae, page, size);
		Integer total = cameraDao.getCamerasTotal(province, city, area, storeName, chineseNmae);
		map.put("key", "success");
		map.put("cameras", cameras);
		map.put("total", total);
		return map;
	}

	@Override
	public Map<String, Object> getCameraInfo(String id) {
		Map<String, Object> map = new HashMap<>();
		if(id == null || id.equals("")){
			map.put("key", "error");
			map.put("msg", "id不可为空");
			return map;
		}
		StoreCameras camera = cameraRepository.getNoDealCamers(id);
		if(camera == null ){
			map.put("key", "error");
			map.put("msg", "设备不存在");
			return map;
		}
		VideoStores stores = storeRepository.findOne(camera.getStoreId());
		if(stores != null){
			camera.setStoreName(stores.getStoreName());
		}
		map.put("key", "success");
		map.put("camera", camera);
		map.put("msg", "设备详情查询成功");
		return map;
	}

	@Override
	public Map<String, Object> dealCamera(String backid,String id) {
		Map<String, Object> map = new HashMap<>();
		if(id == null || id.equals("")){
			map.put("key", "error");
			map.put("msg", "id不可为空");
			return map;
		}
		StoreCameras cameras = cameraRepository.findOne(id);
		if(cameras == null){
			map.put("key", "error");
			map.put("msg", "设备不存在");
			return map;
		}
		if(cameras.getIsUse() == 2){
			map.put("key", "error");
			map.put("msg", "设备已被删除");
			return map;
		}
		if(cameras.getStatus() == 1){
			map.put("key", "error");
			map.put("msg", "设备在使用中，无法删除");
			return map;
		}
		cameras.setIsUse(2);
		cameras = cameraRepository.save(cameras);
		backLogService.addBackLog(backid, "删除摄像头>> 设备id: "+cameras.getId());
		map.put("key", "success");
		map.put("msg", "设备删除成功");
		return map;
	}

}
